<template>
    <div class="checkout-address">
      <div class="text">
        <div class="none" v-if="!showAddress">您需要先添加收货地址才可提交订单。</div>
        <ul v-if="showAddress"> 
          <li><span>收<i/>货<i/>人：</span>{{showAddress.receiver}}</li>
          <li><span>联系方式：</span>{{showAddress.contact}}</li>
          <li><span>收货地址：</span>{{showAddress.fullLocation.replace(/ /g,'')+showAddress.address}}</li>
        </ul>
        <a href="javascript:;" v-if="showAddress" @click="addEdit(showAddress)">修改地址</a>
      </div>
      <div class="action" >
        <XtxButton class="btn" v-if="showAddress" @click="openDialog">切换地址</XtxButton>
        <XtxButton class="btn" @click="addEdit({})">添加地址</XtxButton>
      </div>
    </div>

    <!-- 对话组件 -->
    <!-- 数据双向绑定 -->
    <XtxDialog title="切换收货地址" v-model:visible="visibleDialog">
      <!-- 地址列表 -->
      <div @click="selectedAddress = item" :class="{active: selectedAddress && selectedAddress.id === item.id}"  class="text item" v-for="item in list" :key="item.id">
        <ul>
          <li><span>收<i/>货<i/>人：</span>{{item.receiver}}</li>
          <li><span>联系方式：</span>{{item.contact}}</li>
          <li><span>收货地址：</span>{{item.fullLocation.replace(/ /g,'')+item.address}}</li>
        </ul>
     </div>
    <!-- vue3.0 仅支持v-slot+template写法 -->  
    <template v-slot:footer>
      <XtxButton type="gray" style="margin-right:20px" @click="visibleDialog = true">取消</XtxButton>
      <XtxButton type="primary"  @click="selectedAddressFn">确认</XtxButton>
    </template>
  </XtxDialog>
  <!-- 添加地址--对话框组件 -->
  <AddressEdit @on-success="add" ref="AddressEdit"></AddressEdit>
  </template>
   
  <script>
  import {ref} from 'vue'
  import AddressEdit from './checkout-edit.vue'
  export default {
    name: 'CheckoutAddress',
    components: {AddressEdit},
    props: {
      // 收获地址列表
        list: {
            type: Array,
            default: () => []
        }
    },
    //没有根元素的组件，必须加上emits:['']
    //有根元素的组件，可加可不加
    //emits:['']可清晰看到有多少自定义事件
    emits: ['change'],
    setup (props, {emit}) {
        //有默认地址则显示默认地址
        //没有默认地址，在众多地址里选择第一条为默认地址
        //没有地址数据，去添加地址
        const showAddress = ref(null)
        if(props.list.length) {
            const defaultAddress = props.list.find(item => item.idDefault === 0)
            if(defaultAddress) {
                showAddress.value = defaultAddress
            } else {
                showAddress.value = props.list[0]
            }
        }

        //显示隐藏对话框
        const visibleDialog = ref(false)

        //把地址的id传递给父组件
        emit('change', showAddress.value && showAddress.value.id)

        //记录已选中地址
        const selectedAddress = ref(null)

        const selectedAddressFn = () => {
          //显示地址换成选中地址
          //把选中地址id传给结算组件
          visibleDialog.value = false
          showAddress.value = selectedAddress.value
          emit('change',  selectedAddress.value?.id)
        }

        //打开对话框，清除之前选中的地址
        const openDialog = () => {
          selectedAddress.value =  null
          visibleDialog.value = true
        }

        //添加地址 ---  调用子组件方法
        const AddressEdit = ref(null)
        const addEdit = (addAddress) => {
          AddressEdit.value.open(addAddress)
        }

        //把新增地址添加到收获地址列表
        const add = (newAddress) => {
          const address = props.list.find(item => item.id === newAddress.id)
          if(address) {
            for(const key in address) {
              address[key] = newAddress[key]
            }
          } else {
             // 需要克隆下，不然使用的是对象的引用
          const json = JSON.stringify(newAddress)
          props.list.unshift(JSON.parse(json))
          }
        }



        return {showAddress, visibleDialog, selectedAddress, selectedAddressFn, openDialog, addEdit, AddressEdit, add}
    }
  }
  </script>
  <style scoped lang="less">
  .xtx-dialog {
  .text {
    flex: 1;
    min-height: 90px;
    display: flex;
    align-items: center;
    &.item {
      border: 1px solid #f5f5f5;
      margin-bottom: 10px;
      cursor: pointer;
      &.active,&:hover {
        border-color: @xtxColor;
        background: lighten(@xtxColor,50%);
      }
      > ul {
        padding: 10px;
        font-size: 14px;
        line-height: 30px;
      }
    }
  }
}
  .checkout-address {
    border: 1px solid #f5f5f5;
    display: flex;
    align-items: center;
    .text {
      flex: 1;
      min-height: 90px;
      display: flex;
      align-items: center;
      .none {
        line-height: 90px;
        color: #999;
        text-align: center;
        width: 100%;
      }
      > ul {
        flex: 1;
        padding: 20px;
        li {
          line-height: 30px;
          span {
            color: #999;
            margin-right: 5px;
            > i {
              width: 0.5em;
              display: inline-block;
            }
          }
        }
      }
      > a {
        color: @xtxColor;
        width: 160px;
        text-align: center;
        height: 90px;
        line-height: 90px;
        border-right: 1px solid #f5f5f5;
      }
    }
    .action {
      width: 420px;
      text-align: center;
      .btn {
        width: 140px;
        height: 46px;
        line-height: 44px;
        font-size: 14px;
        &:first-child {
          margin-right: 10px;
        }
      }
    }
  }
  </style>